<template>
  <view class="wrapper" :style="colorStyle">
    <view class="bag">
      <img :src="imgHost+'/statics/images/users/login-bg.jpg'" alt="" srcset="">
    </view>
    <view class="page-msg">
      <view class="title">
        手机号注册
      </view>
    </view>
    <view class="page-form">
      <view class="item acea-row row-between-wrapper">
        <input placeholder='请输入用户名' placeholder-class='placeholder'
               class="codeIput" v-model="nickname"></input>
      </view>
      <view class="item">
        <input type='number' placeholder='请输入手机号码' placeholder-class='placeholder' v-model="phone"
               :maxlength="11"></input>
      </view>
      <view class="item acea-row row-between-wrapper">
        <input type='number' placeholder='请输入验证码' placeholder-class='placeholder' :maxlength="6"
               class="codeIput" v-model="captcha"></input>
        <view class="line">

        </view>
        <button class="code font-num" :class="disabled === true ? 'on' : ''" :disabled='disabled' @click="code">
          {{ text }}
        </button>
      </view>
      <view class="item acea-row row-between-wrapper">
        <input type='password' placeholder='请输入密码' placeholder-class='placeholder'
               class="codeIput" v-model="password"></input>
      </view>
      <view class="btn" @click="submitData">
        立即注册
      </view>
<!--      <view class="btn" @click="goLogin">-->
<!--        手机号登录-->
<!--      </view>-->
      <view @click="goLogin" class="text-center fs-32 text--w111-999 mt-32" >返回手机号登录</view>
    </view>
    <view class="protocol">
      <checkbox-group @change="ChangeIsDefault">
        <checkbox :class="inAnimation?'trembling':''" @animationend='inAnimation=false'
                  v-model:checked="protocol" />
        已阅读并同意
        <text class="main-color" @click.stop="privacy('user')">《用户协议》</text>
        与<text class="main-color" @click.stop="privacy('privacy')">《隐私协议》</text>
      </checkbox-group>
    </view>
    <Verify @success="success" :captchaType="'blockPuzzle'" :imgSize="{ width: '330px', height: '155px' }"
            ref="verify"></Verify>
  </view>
</template>

<script>
import {HTTP_REQUEST_URL} from "@/config/app";

const BACK_URL = "login_back_url";
import colors from '@/mixins/color.js';
import {getCodeApi, mobileRegister, register, registerVerify} from "@/api/user";
import sendVerifyCode from "@/mixins/SendVerifyCode";
import Verify from '../components/verify/verify.vue';
export default {
  name: "Login",
  mixins: [sendVerifyCode, colors],
  components: {
    Verify
  },
  data: function() {
    return {
      imgHost: HTTP_REQUEST_URL,
      inAnimation: false,
      protocol: false,
      nickname:'',
      phone: "",
      password: "",
      captcha: "",
      type: "login",
      keyCode: "",
    };
  },
  watch: {

  },
  onLoad() {

  },
  methods: {
    goLogin(){
      uni.navigateTo({
        url: '/pages/users/login/index'
      })
    },
    ChangeIsDefault() {
      this.$set(this, 'protocol', !this.protocol);
      // console.log('protocol',this.protocol)
    },
    success(data) {
      this.$refs.verify.hide()
      getCodeApi()
          .then(res => {
            this.keyCode = res.data.key;
            this.getCode(data);
          })
          .catch(res => {
            this.$util.Tips({
              title: res
            });
          });
    },
    async getCode(data){
      console.log('data-------',data);
      let that = this;
      if (!that.phone) return that.$util.Tips({
        title: '请填写手机号码'
      });
      if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.phone)) return that.$util.Tips({
        title: '请输入正确的手机号码'
      });
      if (that.formItem == 2) that.type = "register";

      await registerVerify({
        phone: that.phone,
        type: that.type,
        key: that.keyCode,
        captchaType: 'blockPuzzle',
        captchaVerification: data.captchaVerification
      })
          .then(res => {
            that.$util.Tips({
              title: res.msg
            });
            that.sendCode();
          })
          .catch(res => {
            that.$util.Tips({
              title: res
            });
          });
    },
    code() {
      let that = this
      if (!that.protocol) {
        this.inAnimation = true
        return that.$util.Tips({
          title: '请先阅读并同意协议'
        });
      }
      if (!that.phone) return that.$util.Tips({
        title: '请填写手机号码'
      });
      if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.phone)) return that.$util.Tips({
        title: '请输入正确的手机号码'
      });
      // getCodeApi()
      // 	.then(res => {
      // 		that.keyCode = res.data.key;
      // 		that.getCode();
      // 	})
      // 	.catch(res => {
      // 		that.$util.Tips({
      // 			title: res
      // 		});
      // 	});
      this.$refs.verify.show()
    },
    submitData(){
      let that = this;
      if (!that.protocol) {
        this.inAnimation = true
        return that.$util.Tips({
          title: '请先阅读并同意协议'
        });
      }
      if (!that.nickname) return that.$util.Tips({
        title: '请填写用户名'
      })
      if (!that.phone) return that.$util.Tips({
        title: '请填写手机号'
      })
      if (!that.password) return that.$util.Tips({
        title: '请填写密码'
      })

      mobileRegister({
        nickname: that.nickname,
        phone: that.phone,
        password: that.password,
        captcha: that.captcha,
      }).then(res=>{
        if(res.status==200){
          uni.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel:false,
            success: function(res) {
              if (res.confirm) {
                uni.navigateBack({
                  delta: 1
                });
              }
            }
          });
        }
      })
    },
    privacy(type) {
      uni.navigateTo({
        url: "/pages/users/privacy/index?type=" + type
      })
    }
  }
};
</script>

<style lang="scss" scoped>
.wrapper {
  background-color: #fff;
  min-height: 100vh;
  position: relative;

  .bag {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 0;
    /* #ifdef H5 */
    z-index: 0;

    /* #endif */
    img {
      width: 100%;
      height: 544rpx;
    }
  }

  .page-msg {
    padding-top: 160rpx;
    margin-left: 72rpx;
    z-index: 2;
    position: relative;
    .title {
      font-size: 48rpx;
      font-weight: 500;
      color: #333333;
      line-height: 68rpx;
    }

    .tip {
      font-size: 28rpx;
      font-weight: 400;
      color: #333333;
      line-height: 40rpx;
    }
  }

  .page-form {
    width: 606rpx;
    margin: 100rpx auto 0 auto;
    z-index: 2;
    position: relative;
    .item {
      width: 100%;
      height: 88rpx;
      background: #F5F5F5;
      border-radius: 45rpx;
      padding: 24rpx 48rpx;
      margin-bottom: 32rpx;

      input {
        width: 100%;
        height: 100%;
        font-size: 32rpx;
      }

      .placeholder {
        color: #BBBBBB;
        font-size: 28rpx;
      }

      input.codeIput {
        width: 300rpx;
      }

      .line {
        width: 2rpx;
        height: 28rpx;
        background: #CCCCCC;
      }

      .code {
        font-size: 28rpx;
        color: var(--view-theme);
        background-color: rgba(255, 255, 255, 0);
      }

      .code.on {
        color: #BBBBBB !important;
      }
    }

    .btn {
      width: 606rpx;
      height: 88rpx;
      background: var(--view-theme);
      border-radius: 200rpx 200rpx 200rpx 200rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 32rpx;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #FFFFFF;
      line-height: 44rpx;
      margin-top: 48rpx;
      letter-spacing: 1px;
    }
  }
}

.title-bar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 34rpx;
  font-weight: 500;
  color: #333333;
  line-height: 48rpx;
}

.icon {
  position: absolute;
  left: 30rpx;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 80rpx;
  height: 80rpx;

  image {
    width: 35rpx;
    height: 35rpx;
  }
}

.protocol {
  position: fixed;
  bottom: 52rpx;
  left: 0;
  width: 100%;
  margin: 0 auto;
  color: #999999;
  font-size: 24rpx;
  line-height: 22rpx;
  text-align: center;
  bottom: calc(52rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  bottom: calc(52rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/

  .main-color {
    color: var(--view-theme);
  }

  .trembling {
    animation: shake 0.6s;
  }
}

/deep/ uni-checkbox .uni-checkbox-input {
  width: 28rpx;
  height: 28rpx;
}

/deep/ uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked::before {
  font-size: 24rpx;
}

/deep/ uni-checkbox .uni-checkbox-wrapper {
  margin-bottom: 1px;
}

/*checkbox 选项框大小  */
/deep/ checkbox .wx-checkbox-input {
  width: 28rpx;
  height: 28rpx;
}

/*checkbox选中后样式  */
/deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  background: white;
}

/*checkbox选中后图标样式  */
/deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  width: 28rpx;
  height: 28rpx;
  line-height: 28rpx;
  text-align: center;
  font-size: 22rpx;
  background: transparent;
  transform: translate(-50%, -50%) scale(1);
  -webkit-transform: translate(-50%, -50%) scale(1);
}
.appLogin {
  margin-top: 60rpx;

  .hds {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24rpx;
    color: #B4B4B4;

    .line {
      width: 68rpx;
      height: 1rpx;
      background: #CCCCCC;
    }

    p {
      margin: 0 20rpx;
    }
  }

  .btn-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 30rpx;

    .btn {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 68rpx;
      height: 68rpx;
      border-radius: 50%;
    }

    .apple-btn {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 246rpx;
      height: 66rpx;
      margin-left: 30rpx;
      background: #EAEAEA;
      border-radius: 34rpx;
      font-size: 24rpx;

      .icon-s-pingguo {
        color: #333;
        margin-right: 10rpx;
        font-size: 34rpx;
      }
    }

    .iconfont {
      font-size: 40rpx;
      color: #fff;
    }

    .wx {
      background-color: #61C64F;
    }

    .mima {
      background-color: #28B3E9;
    }

    .yanzheng {
      background-color: #F89C23;
    }

    .pingguo {
      margin-left: 60rpx;
      background-color: #000;
    }

  }
}
input{
  background: transparent;
}
button{
  border: none;
  height: auto;
}
</style>
